package com.summer.serialization;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;

public final class Singleton implements Serializable {

	/** */
	private static final long serialVersionUID = 1L;

	private Singleton() {
	}

	private static final Singleton INSTANCE = new Singleton();

	public static Singleton getInstance() {
		return INSTANCE;
	}

	private Object readResolve() throws ObjectStreamException {
		return INSTANCE;
	}

	public static void main(String[] args) throws IOException, Exception {

		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test.serial"));

		Singleton p = Singleton.getInstance();

		oos.writeObject(p);

		oos.close();

		@SuppressWarnings("resource")
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.serial"));

		Singleton p1 = (Singleton) ois.readObject();

		System.out.println(p == p1);

	}

}